using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace SurveyHost.Core.DAL
{
    /// <summary>
    /// Controller class for Stem
    /// </summary>
    [System.ComponentModel.DataObject]
    public partial class StemController
    {
        // Preload our schema..
        Stem thisSchemaLoad = new Stem();
        private string userName = string.Empty;
        protected string UserName
        {
            get
            {
				if (userName.Length == 0) 
				{
    				if (System.Web.HttpContext.Current != null)
    				{
						userName=System.Web.HttpContext.Current.User.Identity.Name;
					}

					else
					{
						userName=System.Threading.Thread.CurrentPrincipal.Identity.Name;
					}

				}

				return userName;
            }

        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public StemCollection FetchAll()
        {
            StemCollection coll = new StemCollection();
            Query qry = new Query(Stem.Schema);
            qry.ORDER_BY("MeasureName ASC, Rank ASC");
            coll.LoadAndCloseReader(qry.ExecuteReader());
            return coll;
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public StemCollection FetchByID(object StemId)
        {
            StemCollection coll = new StemCollection().Where("StemId", StemId).Load();
            return coll;
        }

		
		[DataObjectMethod(DataObjectMethodType.Select, false)]
        public StemCollection FetchByQuery(Query qry)
        {
            StemCollection coll = new StemCollection();
            coll.LoadAndCloseReader(qry.ExecuteReader()); 
            return coll;
        }

        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public bool Delete(object StemId)
        {
            return (Stem.Delete(StemId) == 1);
        }

        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public bool Delete(object StemId, object original_StemId)
        {
            return (Stem.Destroy(original_StemId) == 1);
        }

        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public bool Destroy(object StemId, object original_StemId)
        {
            return (Stem.Destroy(StemId) == 1);
        }

        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public bool Destroy(Stem stem)
        {
            return (Stem.Destroy(stem.StemId) == 1);
        }
        
        
    	
	    /// <summary>
	    /// Inserts a record, can be used with the Object Data Source
	    /// </summary>
        [DataObjectMethod(DataObjectMethodType.Insert, true)]
	    public void Insert(Guid StemId,string MeasureName,string QuestionText,int Rank,string AnswerTypeName,string AnswerTypeConfiguration)
	    {
		    Stem item = new Stem();
		    
            item.StemId = StemId;
            
            item.MeasureName = MeasureName;
            
            item.QuestionText = QuestionText;
            
            item.Rank = Rank;
            
            item.AnswerTypeName = AnswerTypeName;
            
            item.AnswerTypeConfiguration = AnswerTypeConfiguration;
            
	    
		    item.Save(UserName);
	    }

    	
	    /// <summary>
	    /// Updates a record, can be used with the Object Data Source
	    /// </summary>
        [DataObjectMethod(DataObjectMethodType.Update, true)]
	    public void Update(string MeasureName,string QuestionText,int Rank,string AnswerTypeName,string AnswerTypeConfiguration, Guid original_StemId)
	    {
		    Stem item = new Stem();
		    
				item.StemId = original_StemId;
				
				item.MeasureName = MeasureName;
				
				item.QuestionText = QuestionText;
				
				item.Rank = Rank;
				
				item.AnswerTypeName = AnswerTypeName;
				
				item.AnswerTypeConfiguration = AnswerTypeConfiguration;
				
		    item.MarkOld();
		    item.Save(UserName);
	    }

    }

}

